package com.cat.twoPointerTechnique;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/expressive-words/
 * @create 2025/8/5 14:21
 * @since JDK17
 */

public class Solution14 {
    public int expressiveWords(String s, String[] words) {
        int ans = 0, n = s.length(), m = words.length;
        for (int i = 0; i < m; i++) {
            int len = words[i].length(), flag = 1;
            for (int j = 0, k = 0; k < len; ) {
                char a = s.charAt(j), b = words[i].charAt(k);
                if (a != b) {
                    flag = 0;
                    break;
                }
                int p = j, q = k;
                while (j < n && a == s.charAt(j)) {
                    j++;
                }
                while (k < len && b == words[i].charAt(q)) {
                    q++;
                }
                // q - k :
                int s1 = j - p, s2 = q - k;
                if (s1 > s2 || (s2 < s1 && s1 < 3)) {   // 补不了
                    flag = 0;
                    break;
                }
            }
            ans += flag;
        }
        return ans;
    }
}
